package com.gree.changying.server.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Create by 游德禄 on 2019-9-25
 */
public class BeanUtil {
    //属性值拷贝
    // a -> b
    public static void copyPropertie (Object a, Object b){
        Class<?> ca = a.getClass();
        Class<?> cb = b.getClass();
        Field[] fa =  ca.getDeclaredFields();;
        Field[] fb = cb.getDeclaredFields();
        List<Object> childClass = new ArrayList<>();
        for(int j = 0 ;j<fb.length;j++){
            Field t = fb[j];
            t.setAccessible(true);
            for(int i = 0;i < fa.length; i ++){
                Field f = fa[i];
                f.setAccessible(true);
                try{
                    if((!f.getGenericType().toString().contains("java")&&f.getGenericType().toString().contains("."))){
                        if(f.get(a)!=null){
                            childClass.add(f.get(a));
                        }
                    }else{
                        if(!f.getName().equals("id")&&f.getName().equals(t.getName())&&f.getType()==t.getType()){
                            t.set(b, f.get(a));
                        }
                    }
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
        for(Object obj:childClass){
            copyPropertie(obj,b);
        }
    }

}
